/**
 * @file mg_heating.c
 * @author Charry_Le (530673764@qq.com)
 * @brief
 * @version 0.1
 * @date 2024-02-17
 *
 * @copyright Moorgen Intelligent Technologies Copyright (c) 2024
 *
 */
#include "../mg_device.h"
#include "mg_heating.h"
#include "mg_timer.h"

STRUCT_HEAT_VALU Heat;
STRUCT_HEAT_VALU Heat_After_Occupy;
HEAT_CONFIG Heat_Config;

//获取地暖温度最小值
float Get_Heat_TempRange_Min(void)
{
	return (float)Heat_Config.byte.Heat_Temp_Range.Min / 2;
}
//获取温控器温度最大值
float Get_Heat_TempRange_Max(void)
{
	return (float)Heat_Config.byte.Heat_Temp_Range.Max / 2;
}
//获取地暖开关是否使能
uint8_t Get_Heat_Switch_Is_Enable(bool Switch)
{
	if(Switch == ON)
		return Heat_Config.byte.Switch_function.Switch_On_En;
	else
		return Heat_Config.byte.Switch_function.Switch_Off_En;
}
/**********************************************************************/
/******************************地暖控制********************************/
/**********************************************************************/
//地暖开关控制
void Heat_Switch_Set(bool Switch)
{
	if(Heat.Switch == Switch)return;
    Heat.Switch=Switch;
}
//地暖温度控制
void Heat_Temp_Set(float Temperature)
{
	if(Temperature > Get_Heat_TempRange_Max())
		Temperature = Get_Heat_TempRange_Max();
	else if(Temperature < Get_Heat_TempRange_Min())
		Temperature = Get_Heat_TempRange_Min();
	Heat.UserTemp = Temperature;	//用户设置温度
}
//地暖节能
void Heat_ECO_Set(void)
{
	Heat_Temp_Set(DEV_SAVE_TEMP);//节能模式温度
}
//地暖防冻设置
void Heat_AntiFreeze_Set(bool Switch)
{
	if(Switch != ON && Switch != OFF)return;
	Heat.AntiFreeze = Switch;
}
//地暖定时使能设置
void Heat_Timer_Enable_Set(bool Switch)
{
	if(Switch != ON && Switch != OFF)return;
	Heat_Config.byte.TimerEn = Switch;
	Save_Heat_Portion_Config((uint8_t*)&Heat_Config.byte.TimerEn,1);
}
//地暖四时段使能设置
void Heat_Four_Timer_Enable_Set(bool Switch)
{
	if(Switch != ON && Switch != OFF)return;
	Heat_Config.byte.TimeTempEn = Switch;
	Save_Heat_Portion_Config((uint8_t*)&Heat_Config.byte.TimeTempEn,1);
}
//设置地暖开关使能
void Set_Heat_Switch_Enable(bool Switch, bool Enable)
{
	if(Switch == ON && Heat_Config.byte.Switch_function.Switch_On_En != Enable)
	{
		Heat_Config.byte.Switch_function.Switch_On_En = Enable;
	}
	else if(Switch == OFF && Heat_Config.byte.Switch_function.Switch_Off_En != Enable)
	{
		Heat_Config.byte.Switch_function.Switch_Off_En = Enable;
	}
	Save_Heat_Portion_Config((uint8_t*)&Heat_Config.byte.Switch_function,1);
}
//地暖定时逻辑(每分钟调用一次)
void Heat_Timer_Handle(void)
{
	uint8_t i = 0;
	struct tm myTime = Get_Sys_Time();
	if (Heat_Config.byte.TimerEn == ON) //定时开
	{
		for (i = 0; i<TIMER_VALU_MAX; i++)
		{
			if ((myTime.tm_hour == Heat_Config.byte.Timer[1].Hour) && (myTime.tm_min == Heat_Config.byte.Timer[1].Minute))
			{
				Heat.Switch = ON;
			}
			else if ((myTime.tm_hour == Heat_Config.byte.Timer[0].Hour) && (myTime.tm_min == Heat_Config.byte.Timer[0].Minute))
			{
				Heat.Switch = OFF;
			}
		}
	}

	if (Heat_Config.byte.TimeTempEn != ON)return;//四时段定时
	if (myTime.tm_wday >= TIME_TEMP_WEEK_MAX)myTime.tm_wday = 0;
	//0
	if ((myTime.tm_hour == Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[0].Hour) && (myTime.tm_min == Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[0].Min))
	{
		Heat.UserTemp = Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[0].Temp;//时段一
	}
	//1
	if ((myTime.tm_hour == Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[1].Hour) && (myTime.tm_min == Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[1].Min))
	{
		Heat.UserTemp = Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[1].Temp;//时段二
	}

	//2
	if ((myTime.tm_hour == Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[2].Hour) && (myTime.tm_min == Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[2].Min))
	{
		Heat.UserTemp = Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[2].Temp;//时段三
	}

	//3
	if ((myTime.tm_hour == Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[3].Hour) && (myTime.tm_min == Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[3].Min))
	{
		Heat.UserTemp = Heat_Config.byte.TimeTemp[myTime.tm_wday].Term[3].Temp;//时段四
	}
}